//
//  HMStatusForwardView.swift
//  sina
//
//  Created by Kent on 16/9/28.
//  Copyright © 2016年 Ming. All rights reserved.
//

import UIKit

private let margin = CGFloat(8)

class HMStatusForwardView: UIView {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var viewModel: HMStatusViewModel? {
        didSet {
            forwardedContent.text = viewModel?.status?.retweeted_status?.text
            
            let pCount = viewModel?.status?.retweeted_status?.pic_urls?.count ?? 0
            
            pictureCV.snp.updateConstraints { (make) in
                if pCount == 0 {
                    make.top.equalTo(forwardedContent.snp.bottom).offset(margin)
                }
                make.size.equalTo(calculateCVSize(count: pCount))
            }
            pictureCV.pictureInfos = viewModel?.status?.retweeted_status?.pic_urls

        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        UISetup()
    }
    
    func UISetup() {
        
        addSubview(forwardedContent)
        
        forwardedContent.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.top.equalTo(self).offset(margin)
        }
        
        addSubview(pictureCV)
        
        pictureCV.snp.makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.top.equalTo(forwardedContent.snp.bottom).offset(margin)
            make.bottom.equalTo(self)
            make.size.equalTo(CGSize.zero)
        }
        
    }
    
    lazy var forwardedContent: UILabel = {
        let l = UILabel(title: "", fontSize: 13, textColor: UIColor.darkGray)
        l.textAlignment = .left
        return l
    }()
    
    
    lazy var pictureCV: HMPictureCView = {
        
        let lo = UICollectionViewFlowLayout()
        
        lo.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        lo.minimumLineSpacing = pictureCellMargin
        lo.minimumInteritemSpacing = pictureCellMargin
        
        let cv = HMPictureCView(frame: CGRect.zero, collectionViewLayout: lo)
        
        cv.backgroundColor = UIColor.white
        
        return cv
        
    }()
    
    private func calculateCVSize(count: Int) -> CGSize {
        
        if count == 0 {
            return CGSize.zero
        }
        
        if count == 4 {
            let width = itemWidth * 2 + pictureCellMargin
            return CGSize(width: width, height: width)
        }
        
        let rowCount = CGFloat((count - 1) / 3 + 1)
        let height = rowCount * itemWidth + (rowCount - 1) * pictureCellMargin
        
        return CGSize(width: maxWidth, height: height)
    }

}





























